﻿using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Screens.Menu.SelectAvatar;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.ScaleAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Result
{
    public class Bar : Container
    {
        private const float MAX_HEIGHT = 12;
        private const int MAX_ANIMATION_DURATION = 2000;
        private const int DELAY = 500;

        private readonly Label _pointLabel;
        private readonly Timer _pointTimer;

        private readonly int _finalScore;

         public Bar(TeamResult team, float heightPercentage)
         {
             Container barTopContainer = new Container();
             _finalScore = team.TotalScore;

             var barHeight = new ImageContainer {
                                                    Sprite = ResultSheet.GetSprite(ResultSheet.Bar.Center),
                                                    Origin = TE_Rectangle.Alignment.BottomMiddle,
                                                    Scale = new TE_Vector2(2, 0),
                                                    Color = team.Color
                                                };

             barTopContainer.AddChildren(new ImageContainer {
                                                                Position = new TE_Vector2(0, 2),
                                                                Sprite = ResultSheet.GetSprite(ResultSheet.Bar.Top),
                                                                Origin = TE_Rectangle.Alignment.BottomMiddle,
                                                                Color = team.Color,
                                                                Scale = new TE_Vector2(2, 1)
                                                            });

             AddChildren(barHeight, barTopContainer);

             //Raise the bar
             barTopContainer.AddAnimations(new PositionAnimation(new LinearEasing(), new TE_Vector2(0, -MAX_HEIGHT * heightPercentage), (int)(MAX_ANIMATION_DURATION * heightPercentage), false, DELAY));
             barHeight.AddAnimations(new ScaleAnimation(new LinearEasing(), new TE_Vector2(2, MAX_HEIGHT * heightPercentage), (int)(MAX_ANIMATION_DURATION * heightPercentage), false, DELAY));

             for (int i = 0; i < team.Players.Length; i++)
             {
                 var position = new TE_Vector2((i * 2 + 1) / (team.Players.Length * 2.0f) * 160 - 80, -16);
                 barTopContainer.AddChildren(new Avatar(team.Color) { Position = position });

                 var dummy = new Avatar { Visible = false };
                 dummy.Die();
                 barTopContainer.AddChildren(dummy);
             }

             AddChildren(new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, team.Name)
                             {
                                 PositionGridBound = new TE_Vector2(0, 1),
                                 TextAlign = TE_Rectangle.Alignment.CenterMiddle,
                             });

             _pointLabel = new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, "0")
                               {
                                   PositionGridBound = new TE_Vector2(0, -1),
                                   TextAlign = TE_Rectangle.Alignment.CenterMiddle,
                                   Scale = new TE_Vector2(2, 2),
                                   Visible = false,
                               };
             AddChildren(_pointLabel);

             AddTimers(_pointTimer = new Timer(MAX_ANIMATION_DURATION * heightPercentage, false, null));
             CallMethod(DELAY, PointAppear);
         }

        void PointAppear()
        {
            _pointTimer.Start();
            _pointLabel.Visible = true;
        }

        public override void FixedStepUpdate(System.TimeSpan gameTime)
        {
            if (_pointTimer.IsStarted)
            {
                _pointLabel.SetText((_pointTimer.PercentageElapsed * _finalScore).ToString("0"));
            }

            base.FixedStepUpdate(gameTime);
        }
    }
}